package com.example.lxy.controller;

import com.alibaba.fastjson.JSON;
import com.example.config.Result;
import com.example.lxy.pojo.access;
import com.example.lxy.pojo.user;
import com.example.lxy.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.HtmlUtils;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * @author lxy
 */
@Controller
@ResponseBody
@Api(value = "用户管理",tags = "用户接口模块")
public class UserController {
    @Autowired
    private UserService userService;

    @CrossOrigin
    @PostMapping(value = "/api/selectnickname")
    @ApiOperation(value = "根据用户昵称查询",notes = "从用户表中根据昵称查询是否存在该昵称")
    public boolean select1(@RequestBody user u){
        String nickname=u.getNickname();
        user reglist = userService.reglist1(nickname);
        System.out.println(reglist);
        if (reglist!=null){
            return false;
        } else {
            return true;
        }
    }

    @CrossOrigin
    @PostMapping(value = "/api/selectusername")
    @ApiOperation(value = "根据用户名查询",notes = "从用户表中根据用户名查询是否存在该用户")
    public boolean select(@RequestBody user u){
        String username=u.getUsername();
        user reglist = userService.reglist(username);
        System.out.println(reglist);
        if (reglist!=null){
            return false;
        } else {
            return true;
        }
    }

    @CrossOrigin
    @PostMapping(value = "/api/reg")
    @ApiOperation(value = "注册用户",notes = "增加一个用户")
    public boolean add(@RequestBody user u){
        boolean add = userService.add(u);
        return add;
    }

    @CrossOrigin
    @PostMapping(value = "/api/login")
    @ApiOperation(value = "登录",notes = "根据用户名密码来查询是否有该用户")
    public Result login(@RequestBody user u, HttpSession httpSession){
        String username=u.getUsername();
        username= HtmlUtils.htmlEscape(username);
        user lei=userService.login(username,u.getPassword());
        if(lei==null){
            return Result.error();
        }else{
            return Result.ok().data("s",lei);
        }
    }

    @CrossOrigin
    @PostMapping(value = "/api/chacaidan")
    @ApiOperation(value = "动态菜单，权限菜单",notes = "根据用户名多表联查获得动态菜单栏数据")
    public String list(@RequestBody user u){
        HashMap<String, Object> data = new HashMap<>();
        List<user> list = userService.list(u.getNickname());
        if(list==null){
            data.put("flag",400);
        }else{
            data.put("logg",list);
            data.put("flag",200);
        }
        String s = JSON.toJSONString(data);
        return s;
    }

    @CrossOrigin
    @PostMapping(value = "/api/lists")
    @ApiOperation(value = "动态菜单，权限菜单（暂时没有使用）",notes = "存在根据用户显示的BUG")
    public String lists(@RequestBody user u){
        System.out.println(u.getNickname());
        HashMap<String, Object> data = new HashMap<>();
        List<access> lists = userService.lists();
        if(lists==null){
            data.put("flag",400);
        }else{
            data.put("logg",lists);
            data.put("flag",200);
        }
        String s = JSON.toJSONString(data);
        return s;
    }

    @CrossOrigin
    @PostMapping(value = "/api/listbiao")
    @ApiOperation(value = "用户表查询",notes = "两表联查用户和部门表")
    public String listbiao() {
        HashMap<String, Object> data = new HashMap<>();
        List<user> listbiao = userService.listbiao();
        if(listbiao!=null){
            data.put("userbiao",listbiao);
        } else {
            data.put("error",400);
        }
        String s = JSON.toJSONString(data);
        return s;
    }

    @CrossOrigin
    @PostMapping(value = "/api/deluser")
    @ApiOperation(value = "删除用户",notes = "根据uid删除用户")
    public String deluser(@RequestBody user u){
        userService.deluser(u.getUid());
        return "success";
    }

    @CrossOrigin
    @PostMapping(value = "/api/upiduser")
    @ApiOperation(value = "编辑查询",notes = "根据uid查询用户")
    public String upiduser(@RequestBody user u){
        user upiduser = userService.upiduser(u.getUid());
        String s = JSON.toJSONString(upiduser);
        return s;
    }

    @CrossOrigin
    @PostMapping(value ="/api/updateuser")
    @ApiOperation(value = "修改用户",notes = "根据uid查询到的数据进行修改")
    public String updateuser(@RequestBody user u){
        userService.updateuser(u);
        return "success";
    }

    @CrossOrigin
    @PostMapping(value ="/api/likelist")
    @ApiOperation(value = "模糊查询",notes = "根据nickname模糊查询用户表")
    public String likelist(@RequestBody user u){
        String nickname = "%"+u.getNickname()+"%";
        List<user> likelist = userService.likelist(nickname);
        HashMap<String, Object> data = new HashMap<>();
        if (likelist!=null) {
            data.put("likelists",likelist);
            data.put("flag",200);
        } else {
            data.put("flag",400);
        }
        String s = JSON.toJSONString(data);
        return s;
    }
}
